function PlanningManager(target, propDiv){
    var selectedItem = "roundTable";
    this.target = $(target);
    this.propDiv = $(propDiv);
    this.id = 0;

    this.createTrash();

    this.type = "move";
    this.plan = [];
    var instance = this;

    this.target.click(function(handler){
        event.stopPropagation();

        var x = handler.offsetX;
        var y = handler.offsetY;

        instance.factoryObject(instance.type, {}, x, y);
    });
}

PlanningManager.prototype.clear = function(){
    this.plan = [];
    this.target.empty();
    this.createTrash();
}


PlanningManager.prototype.factoryObject = function (type, state, x, y){
    var item;
    var resizable = false;
    switch(type){
        case ROUND_TABLE:
            item = new RoundTable(state);
            break;
        case SEAT:
            item = new Seat(state);
            break;
        case CUSTOM_OBJECT:
            item = new CustomObject(state);
            resizable = true;
            break;
        default:
            return;
    }
    var placeholder = new PlanningPlaceholder({
        position: {top: y, left: x},
        resizable: resizable,
        item: item,
        manager: this
    });

    this.plan.push({
        placeholder: placeholder,
        type: type
    });

    placeholder.appendTo(this.target);
}

PlanningManager.prototype.save = function (){
    var plan = [];
    for(var i = 0; i < this.plan.length; i++){
        var planItem = this.plan[i];
        plan.push({
            position: planItem.placeholder.state.position,
            type: planItem.type,
            state: planItem.placeholder.state.item.state
        })
    }
    $.ajax({
        url: "?action=save",
        dataType: "JSON",
        type: "POST",
        data: {
            data: plan
        },
        success: function (){
            alert("Plán bol uložený")
        },
        error: function (data){
            console.log(data);
        }
    });
}

PlanningManager.prototype.submit = function (){
    var plan = [];
    for(var i = 0; i < this.plan.length; i++){
        var planItem = this.plan[i];
        plan.push({
            position: planItem.placeholder.state.position,
            type: planItem.type,
            state: planItem.placeholder.state.item.state
        })
    }
    $.ajax({
        url: "?action=submit",
        dataType: "JSON",
        type: "POST",
        data: {
            data: plan
        },
        success: function (){
            alert("Plán bol odovzdaný")
        },
        error: function (data){
            console.log(data);
        }
    });
}


PlanningManager.prototype.load = function (){
    this.clear();
    var instance = this;
    $.get("./resources/tempTables.json", {dataType: "JSON"},
        function(data){
            if (!$.isArray(data)){
                data = $.parseJSON(data);
            }
            for(var i = 0; i < data.length; i++){
                var planItem = data[i];
                var x = parseInt(planItem.position.left);
                var y = parseInt(planItem.position.top);
                instance.factoryObject(planItem.type, planItem.state, x, y);
            }
        }
    );
}

PlanningManager.prototype.drag = function (element, ui){
    if (ui.position.top < 5){
        this.trash.show();
    } else {
        this.trash.hide();
    }
}

PlanningManager.prototype.dragStop = function (element, ui){
    this.trash.hide();
    if (ui.position.top < 5){
        for(var i = 0; i < this.plan.length; i++){
            if (this.plan[i].placeholder == element){
                this.plan.splice(i, 1);
                break;
            }
        }
        element.delete();
    }
}

PlanningManager.prototype.createTrash = function (){
    this.trash = $("<div />");
    this.trash.css("position", "absolute");
    this.trash.css("width", "100%");
    this.trash.css("height", "20px");
    this.trash.css("background-color", "red");
    this.trash.css("z-index", 1000);
    this.trash.css("display", "none");
    this.trash.css("opacity", 0.5);
    this.trash.appendTo(this.target);
}

PlanningManager.prototype.showProperties = function (item, propDiv){
    var object = item.state;
    var objectPlaceholder = $("<div class='ui-widget-content ui-corner-all' style='margin: 4px;'/>");

    var addProperty = function (key, value, container){
        var type = $.type(value);
        if (type == "string" || type == "number"){
            var id = this.id++;
            id = "generated_prop_" + id;
            var label = new $("<label for='" + id + "'/>");
            label.text(key + ":");
            var input = new $("<input id='" + id + "'/>");
            input.val(value);
            input.change(function(){
                object[key] = input.val();
                item.setState(object);
            });
            container.append(label);
            container.append(input);
        } else if (type == "object"){
            var div = objectPlaceholder.clone();
            var header = $("<h3 class='ui-widget-header' style='margin-top: 0; margin-bottom: 0; text-align: center;'/>");
            header.text(key);
            div.append(header);
            for(var i in value){
                addProperty(i, value[i], div);
            }
            div.appendTo(container);
        }
    };

    propDiv.empty();
    for(var key in object){
        var value = object[key];
        addProperty(key, value, propDiv);
    }
};

PlanningManager.prototype.select = function (element){
    this.showProperties(element.state.item, this.propDiv);
}